<?php

    /**
    * Node Controller.
    *
    * Helpful methods to make Controller.
    *
    * PHP versions 5
    * CAKEPHP versions 2.x
    *
    * Green CMS - Content Management System and Framework Powerfull by Cakephp
    * Copyright 2012, GREEN GLOBAL CO., LTD (toancauxanh.vn)
    *
    * CakePHP(tm) :  Rapid Development Framework (http://www.cakephp.org)
    * Copyright 2005-2011, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
    *
    * Licensed under The MIT License
    * Redistributions of files must retain the above copyright notice.
    *
    * @author        Technology Lab No.I <tech1@toancauxanh.vn>
    * @link
    * @package       NodeType.Controller
    * @since         Green v 1.0
    * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
    */
    App::uses('NodeAppController', 'Node.Controller','User.UserRole','Node.ManagementLanguage','Node.Action','Node.Subsite','Node.ConfigMail','Node.Contact','Node.Question','Node.MoveNode');
    App::uses('ViewController', 'View.Controller');
    App::uses('my_image', 'Vendor');
    App::uses('Controller', 'Controller');
    Class NodeController extends NodeAppController {

        /**
        * Use model class
        *
        * @var array
        */
        public $uses = array('Node.Node','Node.NodeTerm','Node.Term', 'Node.Gallery','Node.NodeType',
            'Node.NodeRole','Node.ManagementLanguage','Node.Action','Node.Subsite','Node.ConfigMai','Node.Contact','Node.Question',
            'Node.MoveNode','Meta');

        /**
        * Use model class
        *
        * @var array
        */
        public $helpers = array('Text','String','Js');       
        /**
        * Called after the controller action.
        *
        * @return void
        */
        public function beforeRender() {  
            parent::beforeRender();
            if (in_array($this->request['action'], array('admin_add', 'admin_edit'))) {
                $layouts = $this->Dashboard->get($this->Node, 'layouts');            
                if (empty($layouts)) {
                    $layouts = array(null, array_keys($this->Node->schema()));
                }            
                $this->set('_layouts', $layouts);            
            }        
            if (in_array($this->request['action'], array('admin_edit_management','admin_management_language','admin_order_edit'))) {                        
                $layouts = $this->Dashboard->get($this->ManagementLanguage, 'layouts');            
                if (empty($layouts)) {
                    $layouts = array(null, array_keys($this->ManagementLanguage->schema()));               
                }
                $this->set('_layouts', $layouts);

            }        
            if (in_array($this->request['action'], array('admin_config_action','admin_edit_config_action'))) {                        
                $layouts = $this->Dashboard->get($this->Action, 'layouts');              
                if (empty($layouts)) {
                    $layouts = array(null, array_keys($this->Action->schema()));               
                }
                $this->set('_layouts', $layouts);            
            } 
            if (in_array($this->request['action'], array('admin_reply'))) {
                $layouts = $this->Dashboard->get($this->Node, 'layouts_reply');
                if (empty($layouts)) {
                    $layouts = array(null, array_keys($this->Node->schema()));
                }
                $this->set('_layouts', $layouts);
            }
        }    
        public function beforeFilter() {
            // open new
            parent::beforeFilter();
            if (in_array($this->request['action'], array('admin_add','admin_edit_item','admin_edit','delete_image', 'reply','admin_list_index','admin_management_language','admin_config_action','admin_management_subsite','admin_index','admin_send_mail','admin_mn_comment','admin_manage_config_mail','admin_manage_contact','admin_manage_question','admin_answer_question','admin_edit_contact','admin_manage_contact','admin_view_node','admin_move_node','admin_add_question'))) {
                $this->Components->disable('Security');

            }
            if (in_array($this->request['action'],array('admin_index','admin_list_index'))) 
            {                                                   
            } 
            if (in_array($this->request['action'], array('admin_edit'))) {                
            } 
            if (in_array($this->request['action'], array('admin_delete','admin_delete_list_index'))) {                           
            }     
        }
        public function insertNodeRole($nodeID = null){
            $listRole = $_SESSION['listRole'];
            $is_administrator = $_SESSION['isAdministrator'];                
            if(!$is_administrator){
                $data['NodeRole']['node_id'] = $nodeID; 
                if(!empty($listRole))
                {
                    foreach($listRole as $k=>$v)
                    {
                        $this->NodeRole->create();                    
                        $data['NodeRole']['role_id'] = $v;                    
                        $this->NodeRole->save($data['NodeRole']);
                    }    
                }
                else
                {                
                    $data['NodeRole']['role_id'] = 1; 
                }   
            }                      
        }  
        /**
        * Management data
        *
        * @return void
        */
        public function admin_index() {                                                      
            $_paginate = array(                            
                'order' => array('id' => 'desc'),
                'fields' => array('id')
            );         
            $conditions = $this->Dashboard->parseRequest($this->Node, $_paginate, array('title'));           
            $listNodes= $this->Node->find('all',
                array('order'=>'Node.created DESC'));        
            $nodeTypes = $this->Node->NodeType->find('list');
            $users = $this->Node->User->find('list');
            $this->Dashboard->translateMaps($this->Node, $listNodes);
            $this->Dashboard->toolbar(__d('system', 'Content management'), array(
                /*'add' => array('action' => 'add', 'text' => __d('system', 'Add')),*/
                'edit' => array('action' => 'edit', 'text' => __d('system', 'Edit')),
                'remove' => array('action' => 'removes', 'text' => __d('system', 'Delete'))
            ));
            if (!empty($this->data['Paginate'])) {
                $data = array_merge(array('task' => '', 'id' => array(0)), $this->data['Paginate']);
                switch (strtolower($data['task'])) {
                    case 'add': {
                        $this->Dashboard->redirect(array('plugin'=>'node','controller'=>'node','action' => 'add', $data['id'][0]));
                    }
                    case 'edit': {
                        $this->Dashboard->redirect(array('plugin'=>'node','controller'=>'node','action' => 'edit', $data['id'][0]));
                    }
                    case 'remove': {
                        $this->Dashboard->redirect(array('plugin'=>'node','controller'=>'node','action' => 'delete', '?' => array('keys' => $data['id'])));
                    }
                }                 
            }        
            $this->set(compact('listNodes','nodeTypes','users')); 
        }
        public function admin_send_mail($node_type_id = null,$node_id=null){
            $this->Dashboard->toolbar(__d('system', 'Content management'), array(
                'add' => array('action' => 'add', 'text' => __d('system', 'Send Mail')),            
            ));
            $groupUser = $list_email = $first_key = array();
            $groupUser = $this->Term->find('list',array(
                'conditions'=>array('Term.type_id'=>Register_Letter)
            ));        
            if(!empty($groupUser)){
                $first_key = key($groupUser);
            }
            $list_email = $this->NodeTerm->find('all',array(
                'conditions'=>array('NodeTerm.term_id'=>$first_key),
                'fields'=>array('Node.id','Node.title','Node.alias','Node.field_email')
            ));
            $node = $this->Node->find('first',array(
                'conditions'=>array('Node.id'=>$node_id),
                'fields'=>array('Node.id','Node.alias','Node.title','Node.field_description','Node.field_body')
            )); 
            $action = $serverName = $content = null;
            $serverName = $_SERVER['SERVER_NAME'];
            $action .= $serverName;
            $action .= $this->routerAction($node_type_id);
            $action .= '/'.$node['Node']['alias'].'.html';                        
            if (!empty($this->data['Paginate'])) {            
                $data = array_merge(array('task' =>$node_id, 'id' => array(0)), $this->data['Paginate']);
                switch (strtolower($data['task'])) {
                    case 'add': {
                        $this->loadModel('View.Contact');
                        $this->loadModel('View.Node');
                        $this->loadModel('Node.Node');                                                 
                        $data = $this->request->data;
                        $config_mail = Configure::read('App.themes.config_mail');
                        $Email = $this->Components->load('Email');                                                          
                        if(!empty($config_mail)){
                            $config_mail = array_shift($config_mail);                
                            $port = $config_mail['config_mail']['port'];
                            $timeout = $config_mail['config_mail']['timeout'];
                            $host = $config_mail['config_mail']['host'];
                            $username = $config_mail['config_mail']['username'];
                            $password = $config_mail['config_mail']['password'];
                            $sendAs = $config_mail['config_mail']['sendAs'];
                            $prefix = $config_mail['config_mail']['prefix'];
                            $from = $prefix.' '.$config_mail['config_mail']['from'];
                            $to = explode(';',$config_mail['config_mail']['to']); 
                            if(!empty($to)){
                                $to = array_map('trim',$to);   
                            }                                          
                            $subject = $prefix. ' ' . $config_mail['config_mail']['subject'];              
                        }else{
                            $port = 465;
                            $timeout = 30;
                            $host = 'ssl://smtp.gmail.com';
                            $username = 'vantu06i@gmail.com';
                            $password = 'dungcomo'; 
                            $sendAs = 'html';
                            $prefix = "[DHNN]";
                            $to = array();
                            $from = $prefix.' <tunv@appdev.vn>';                
                            $subject = $prefix.' Co thong tin lien he';   
                        }   
                        if(!empty($data['Node']['email'])){                        
                            $Email = $this->Components->load('Email');
                            if((empty($config_mail))||($config_mail['config_mail']['type_mail']==1)){
                                $Email->smtpOptions = array(
                                    'port'=>$port,
                                    'timeout'=>$timeout,
                                    'host' => $host,
                                    'username'=>$username,
                                    'password'=>$password,
                                );
                                $Email->delivery = 'smtp';    
                            }                            
                            $Email->subject = ($subject);                    
                            $Email->sendAs = $sendAs;                            
                            $Email->from = $from; 
                            if(!empty($to)){                        
                                $to = array_merge($to,array_values($data['Node']['email'])); 
                                $to = array_unique($to);
                            }else{
                                $to = $data['Node']['email'];
                            }
                            $Email->to = $to;                            
                            $content = __d('system','Chào Bạn ')."<br/>";
                            $content .= __d('system','Bạn nhận được thông tin chia sẻ bài viết từ quản trị hệ thống ')."<br/>";
                            $content .= __d('system','Mời bạn click vào link bên dưới để xem chi tiết ')."<br/>";
                            $content .= "<a href=".$action.">";
                            $content .= " ".$node['Node']['title'];
                            $content .= "</a>";                             
                            $Email->subject = $subject;                                                                                                                                            
                            $Email->send($content);                                                        
                        }                                                                    
                    }              
                }            
            }          
            $this->set(compact('groupUser','list_email'));
        }
        public function load_email_register($term_id=null){
            $list_email = array();
            $this->layout = 'ajax';
            $list_email = $this->NodeTerm->find('all',array(
                'conditions'=>array('NodeTerm.term_id'=>$term_id),
                'fields'=>array('Node.id','Node.title','Node.alias','Node.field_email')
            ));
            $this->set(compact('list_email')); 
        }
        /**
        * Management data
        *
        * @return void
        */
        public function admin_list_index($id = null,$term_id=null) {
            $conds = array(); 
            $default_show = 0;
            $_listSubSite = $_SESSION['_listSubSite'];        
            $subsite = $this->NodeType->find('first',array(
                'conditions'=>array('NodeType.sub_site'=>$_listSubSite,'NodeType.id'=>$id)
            ));            
            $is_administrator = $_SESSION['isAdministrator'];
            if((!$is_administrator)&&(empty($subsite))){
                echo __d('system',"Lỗi: Bạn không có quyên truy cập module này !!!");die;
            }
            if(empty($term_id)){
                $conds = array_merge($conds,array('Node.node_type_id'=>$id));             
            } 
            else{                      
                $this->loadModel('Node.NodeTerm');
                $this->loadModel('Node.Term');
                $listNodes = $this->NodeTerm->find('list',array(
                    'conditions'=>array('NodeTerm.term_id'=>$term_id),
                    'fields'=>array('NodeTerm.id','NodeTerm.node_id')                
                ));             
                if(!empty($listNodes)){
                    $listNodes = array_values($listNodes);
                    $conds = array_merge($conds,array('Node.id'=>$listNodes));
                }else{$conds = array('Node.id'=>-1);} 
                $term_name=$this->Term->find('first',
                    array('conditions'=>array('Term.id'=>$term_id)));
                $this->set(compact('term_name'));                         
            }                                                             
            $_paginate = array(
                'order' => array('created' => 'desc'),
                'fields' => array('id'),
                'locale'=>true,
                'conditions'=>$conds            
            );                         
            $listNodes = $this->Node->find('all',array(
                'conditions'=>$conds,
                'order' => array('Node.created' => 'desc')            
            ));
            //for load move nodes
            $subsite_current = Configure::read('App.themes.subsite_current');
            if(empty($subsite_current)){
                $subsite_current['id'] = -1; 
            } 
            //End
            $kind_node_type = $this->NodeType->find('first',array('conditions'=>array('NodeType.id'=>$id),'recursive'=>-1));                         
            if(empty($kind_node_type)){
                $kind_node_type['NodeType']['kind_node_type_id'] = -1;
            }
            $moveNodes = $this->MoveNode->find('all',array(
                'conditions'=>array('KindNodeType.id'=>$kind_node_type['NodeType']['kind_node_type_id'])
            ));                                 
            $this->set(compact('moveNodes'));                 
            $conditions = $this->Dashboard->parseRequest($this->Node, $_paginate, array('title'));        
            $nodeTypes = $this->Node->NodeType->find('list');                             
            $users = $this->Node->User->find('list');
            $list_names = $this->Node->NodeType->find('first',array(
                'conditions'=>array('id'=>$id),
                'locale'=>true,
                'fields'=>array('NodeType.id','NodeType.name'),
            ));
            $this->Dashboard->translateMaps($this->Node, $listNodes);
            $this->Dashboard->toolbar(__d('system', 'Content management'), array(
                'add' => array('action' => 'add', 'text' => __d('system', 'Add')),
                'edit' => array('action' => 'edit', 'text' => __d('system', 'Edit')),
                'remove' => array('action' => 'removes', 'text' => __d('system', 'Delete'))
            ));       
            if (!empty($this->data['Paginate'])) {
                $data = array_merge(array('task' =>$id, 'id' => array(0)), $this->data['Paginate']);
                switch (strtolower($data['task'])) {
                    case 'add': {
                        $this->Dashboard->redirect(array('plugin'=>'node','controller'=>'node','action' => 'add',$id,$term_id));
                    }
                    case 'edit': {
                        $this->Dashboard->redirect(array('plugin'=>'node','controller'=>'node','action' => 'edit', $id));
                    }
                    case 'remove': {
                        $this->Dashboard->redirect(array('plugin'=>'node','controller'=>'node','action' => 'delete', '?' => array('keys' => $id)));
                    }
                }
                $dataPaginate = $this->request->data;                       
                if(!empty($dataPaginate['Paginate']['chk_node_type'])){                
                    $chk_node_type = $dataPaginate['Paginate']['chk_node_type'];
                    foreach($chk_node_type as $k=>$v){
                        $this->Node->id = $v;
                        if($this->Node->exists()){                       
                            $this->Node->delete();
                        }                     
                    }                
                    $this->Dashboard->redirect(array('action' => 'admin_list_index',$id,'ext' => false), true);
                }
                if(!empty($dataPaginate['Paginate']['show_home'])){
                    $show_home = intval($dataPaginate['Paginate']['show_home']);
                    $default_show = $show_home;
                    $conds = array_merge($conds,array('Node.field_is_home'=>$show_home));               
                    $listNodes = $this->Node->find('all',array(
                        'conditions'=>$conds,
                        'order' => array('Node.created' => 'desc')            
                    ));  
                }             
            }        
            $nodeType=$kind_node_type;
            $terms=$this->Term->find('list',
                array('conditions'=>array('Term.type_id'=>$id)));
            $this->set(compact('listNodes','nodeTypes','nodeType','terms','term_id','users','id','list_names','default_show'));
        }
        /**
        * Add data
        *
        * @return void
        */
        public function admin_add($typeId = null, $term_id = null) {            
            $listRole = -1;
            $listRole = $_SESSION['listRole'];                       
            if (empty($typeId)) {
                $this->Dashboard->exists($this->Node->NodeType, $typeId, false);
                $this->Dashboard->toolbar(__d('system', 'Add new Content'), array());
                $listTypes = $this->Node->NodeType->find('all', array('fields' => array('id', 'name', 'type')));
                $this->set(compact('listTypes', 'typeId'));
            } else {
                $this->loadModel('Node.Field');
                $fields = $this->Field->find('all', array('recursive' => -1, 'order' => 'id ASC', 'conditions' => array('type_id' => $typeId)));
                if (!empty($fields)) {
                    foreach ($fields as &$field) {
                        $field['Field']['params'] = unserialize($field['Field']['params']);
                    }
                }                
                $this->loadModel('Node.Term');
                $list_terms = $this->Term->generateTreeList(array('Term.type_id' => $typeId), null, null, '&nbsp;&nbsp;&nbsp;|-&nbsp;');            
                $node_type = $this->NodeType->read(null,$typeId);
                if(empty($node_type)){
                    $this->Dashboard->redirect(array('action' => 'index', 'ext' => false), true); 
                }
                if($node_type['NodeType']['set_image']==1){
                    $this->set('images', 'images');
                }            
                $this->set(compact('list_terms'));
                $user = $this->Auth->user();
                $type = $this->Node->NodeType->findById($typeId);
                $this->Dashboard->exists($this->Node, '', false);
                $this->Dashboard->toolbar(__d('system', 'Add new Content'), array(
                    'save' => array('action' => 'save', 'text' => __d('system', 'Save')),
                    'next' => array('action' => 'save', 'text' => __d('system', 'Save & Add New')),
                    'cancel' => __d('system', 'Cancel')
                ));
                if (!empty($this->request->data)) {   //debug($this->data);exit;             
                    $this->Node->create();
                    $task = strtolower($this->data['Paginate']['task']);
                    if (!$task || $task === 'cancel') {
                        $this->Dashboard->redirect(array('action' => 'index', 'ext' => false), true);
                    } elseif (!empty($this->request->data)) {
                        $data = $this->request->data;                                                                               
                        if ($this->Node->save($this->request->data)) 
                        {   

                            if (!empty($this->request->data['Node']['term_id'])) {
                                $term = array();
                                $term['NodeTerm']['node_id'] = $this->Node->id;
                                $term['NodeTerm']['term_id'] = $this->request->data['Node']['term_id'];
                                $this->Node->NodeTerm->save($term);
                            }                             
                        }
                        if(!empty($data['ProImage']))
                        {   
                            $image_rels= $data['ProImage'];
                            if(!empty($image_rels))
                            {
                                $id= $this->Node->id;
                                $images= $image_rels;
                                foreach($images as $key=> $value){
                                    if(!empty($value['path'])){
                                        $this->Gallery->create();
                                        $gallery['Gallery']['node_id']= $id;
                                        $gallery['Gallery']['path']= $value['path'];
                                        $gallery['Gallery']['priority']= $value['priority'];
                                        $this->Gallery->save($gallery);
                                    }
                                }
                            }
                        }                                             
                        $this->setFlash(__d('system', 'The %s has been saved', __d('system', 'Node')), 'success');
                        switch ($task) {
                            case 'next': {
                                $this->redirect($this->request->here(false));
                            }
                            case 'apply': {
                                $this->Dashboard->redirect(array('action' => 'edit', $this->Node->getInsertID()), false, true);
                            }
                        }

                        $this->Dashboard->redirect(array('action' => 'index', 'ext' => false), true);
                    } else {
                        $this->setFlash(__d('system', 'The %s could not be saved. Please, try again.', __d('system', 'Node')), 'error');
                    }
                } //elseif ($id) {
                //                    $this->request->data = $this->Node->find('first', array('conditions' => array('Node.id' => $id), 'recursive' => -1));
                //                }
                $groupUser = $list_email = $first_key = array();
                $groupUser = $this->Term->find('list',array(
                    'conditions'=>array('Term.type_id'=>Register_Letter)
                ));        
                if(!empty($groupUser)){
                    $first_key = key($groupUser);
                }
                if(!empty($groupUser)){
                    $first_key = key($groupUser);
                }
                $list_email = $this->NodeTerm->find('all',array(
                    'conditions'=>array('NodeTerm.term_id'=>$first_key),
                    'fields'=>array('Node.id','Node.title','Node.alias','Node.field_email')
                ));
                // $nodeType=$type;debug($list_terms);exit();
                if(!$list_terms){
                    $term_type="hidden";
                }else{  $term_type='select';}

                $this->set(compact('id', 'user','term_id', 'type','indexs','term_type', 'fields', 'layoutFields', 'fields', 'typeId','groupUser','list_email'));
            }
        }

        /**
        * Edit data
        *
        * @return void
        */
        public function admin_edit($typeId, $id = null) {        
            $this->Dashboard->exists($this->Node, $id);
            $this->Dashboard->toolbar(__d('system', 'Edit Node #%s', $id), array(
                'save' => array('action' => 'save', 'text' => __d('system', 'Save')),
                'remove' => array('action' => 'remove', 'text' => __d('system', 'Delete')),
                'cancel' => __d('system', 'Cancel')
            ));
            $datas = $this->Node->find('first', array('conditions' => array('Node.id' => $id)));
            $this->loadModel('Node.Field');
            $fields = $this->Field->find('all', array('recursive' => -1, 'order' => 'id ASC', 'conditions' => array('type_id' => $typeId)));
            if (!empty($fields)) {
                foreach ($fields as &$field) {
                    $field['Field']['params'] = unserialize($field['Field']['params']);
                }
            }
            $this->loadModel('Node.Term');
            $listTerms = $this->Term->generateTreeList(array('Term.type_id' => $typeId), null, null, '&nbsp;&nbsp;&nbsp;|-&nbsp;');
            $node_type = $this->NodeType->read(null,$typeId);
            if(empty($node_type))
            {
                $this->Dashboard->redirect(array('action' => 'index', 'ext' => false), true); 
            }
            if($node_type['NodeType']['set_image']==1)
            {

                $galleries= $this->Gallery->find('all', array(
                    'conditions'=> array('Gallery.node_id'=> $id),
                    'fields'=> array('Gallery.id', 'Gallery.path','Gallery.priority')
                ));
                $this->set('galleries', $galleries); 

                $this->set('images', 'images');
            }        
            $nodeTerm = $this->Node->NodeTerm->find('first', array('conditions' => array('NodeTerm.node_id' => $id)));
            $this->set(compact('listTerms', 'nodeTerm'));
            if (!empty($this->request->data)) {
                $task = strtolower($this->data['Paginate']['task']);             
                if (!$task || $task === 'cancel') {
                    $this->Dashboard->redirect(array('action' => 'index', 'ext' => false), true);
                } elseif ($this->Node->save($this->request->data)) {               
                    $data= $this->request->data;


                    if(!empty($data['ProImage'])){
                        $id= $this->Node->id;
                        $images= $data['ProImage'];
                        foreach($images as $key=> $value){
                            if(!empty($value['path'])){
                                $this->Gallery->create();
                                $gallery['Gallery']['node_id']= $id;
                                $gallery['Gallery']['path']= $value['path'];
                                $gallery['Gallery']['priority']= $value['priority'];
                                $this->Gallery->save($gallery);
                            }
                        }
                    }
                    if (!empty($this->request->data['Node']['term_id'])) {
                        $term = array();
                        $term['NodeTerm']['id'] = $this->request->data['Node']['node_term_id'];
                        $term['NodeTerm']['node_id'] = $id;
                        $term['NodeTerm']['term_id'] = $this->request->data['Node']['term_id'];
                        $this->Node->NodeTerm->save($term);
                    }else{
                        $this->NodeTerm->id = $this->request->data['Node']['node_term_id'];
                        $this->NodeTerm->delete();
                    }                   
                    if ($task !== 'remove') {
                        $this->setFlash(__d('system', 'The %s has been updated', __d('system', 'Node')), 'success');
                    }
                    switch ($task) {
                        case 'remove': {
                            $this->Dashboard->redirect(array('action' => 'delete', '?' => array('keys' => $id)), false, true);
                        }
                        case 'apply': {
                            $this->redirect($this->request->here(false));
                        }
                    }
                    $this->Dashboard->redirect(array('action' => 'index', 'ext' => false), true);
                } else {
                    $this->setFlash(__d('system', 'The %s could not be updated. Please, try again.', __d('system', 'Node')));
                }
            } else {
                $data = $this->Node->find('first', array('conditions' => array('Node.id' => $id)));
                $this->request->data=$data;

            }
            $groupUser = $list_email = $first_key = array();
            $groupUser = $this->Term->find('list',array(
                'conditions'=>array('Term.type_id'=>Register_Letter)
            ));        
            if(!empty($groupUser)){
                $first_key = key($groupUser);
            }
            if(!empty($groupUser)){
                $first_key = key($groupUser);
            }
            $list_email = $this->NodeTerm->find('all',array(
                'conditions'=>array('NodeTerm.term_id'=>$first_key),
                'fields'=>array('Node.id','Node.title','Node.alias','Node.field_email')
            ));
            $nodeType=$this->NodeType->find('first',
                array('conditions'=>array('NodeType.id'=>$typeId),'recursive'=>-1));
            if(!$listTerms){
                $term_type="hidden";
            }else{  $term_type='select';}
            $this->set(compact('id', 'fields','indexs','typeId','datas','groupUser','nodeType','list_email','term_type'));
        }
        public function admin_view_node($typeId, $id = null) {
            $this->Dashboard->exists($this->Node, $id);
            $this->Dashboard->toolbar(__d('system', 'Edit Node #%s', $id), array(                    
                'cancel' => __d('system', 'Back')
            ));
            $datas = $this->Node->find('first', array('conditions' => array('Node.id' => $id)));
            $this->loadModel('Node.Field');
            $fields = $this->Field->find('all', array('recursive' => -1, 'order' => 'id ASC', 'conditions' => array('type_id' => $typeId)));
            if (!empty($fields)) {
                foreach ($fields as &$field) {
                    $field['Field']['params'] = unserialize($field['Field']['params']);
                }
            }
            $this->loadModel('Node.Term');
            $listTerms = $this->Term->generateTreeList(array('Term.type_id' => $typeId), null, null, '&nbsp;&nbsp;&nbsp;|-&nbsp;');
            $node_type = $this->NodeType->read(null,$typeId);
            if(empty($node_type))
            {
                $this->Dashboard->redirect(array('action' => 'index', 'ext' => false), true); 
            }
            if($node_type['NodeType']['set_image']==1)
            {

                $galleries= $this->Gallery->find('list', array(
                    'conditions'=> array('Gallery.node_id'=> $id),
                    'fields'=> array('Gallery.id', 'Gallery.path')
                ));
                $this->set('galleries', $galleries); 

                $this->set('images', 'images');
            }        
            $nodeTerm = $this->Node->NodeTerm->find('first', array('conditions' => array('NodeTerm.node_id' => $id)));
            $this->set(compact('listTerms', 'nodeTerm'));
            if (!empty($this->request->data)) {
                $task = strtolower($this->data['Paginate']['task']);             
                if (!$task || $task === 'cancel') {
                    $this->Dashboard->redirect(array('action' => 'index', 'ext' => false), true);
                } elseif ($this->Node->save($this->request->data)) {               
                    $data= $this->request->data;
                    if(!empty($data['ProImage'])){
                        $image_rels= $data['ProImage'];
                    }
                    if(!empty($image_rels)){
                        $id= $this->Node->id;
                        $images= $image_rels;
                        foreach($images as $key=> $value){
                            if(!empty($value)){
                                $this->Gallery->create();
                                $gallery['Gallery']['node_id']= $id;
                                $gallery['Gallery']['path']= $value;
                                $this->Gallery->save($gallery);
                            }
                        }
                    }
                    if (!empty($this->request->data['Node']['term_id'])) {
                        $term = array();
                        $term['NodeTerm']['id'] = $this->request->data['Node']['node_term_id'];
                        $term['NodeTerm']['node_id'] = $id;
                        $term['NodeTerm']['term_id'] = $this->request->data['Node']['term_id'];
                        $this->Node->NodeTerm->save($term);
                    }else{
                        $this->NodeTerm->id = $this->request->data['Node']['node_term_id'];
                        $this->NodeTerm->delete();
                    }
                    if ($task !== 'remove') {
                        $this->setFlash(__d('system', 'The %s has been updated', __d('system', 'Node')), 'success');
                    }
                    switch ($task) {
                        case 'remove': {
                            $this->Dashboard->redirect(array('action' => 'delete', '?' => array('keys' => $id)), false, true);
                        }
                        case 'apply': {
                            $this->redirect($this->request->here(false));
                        }
                    }
                    $this->Dashboard->redirect(array('action' => 'index', 'ext' => false), true);
                } else {
                    $this->setFlash(__d('system', 'The %s could not be updated. Please, try again.', __d('system', 'Node')));
                }
            } else {
                $this->request->data = $this->Node->find('first', array('conditions' => array('Node.id' => $id)));
            }
            $this->set(compact('id', 'fields','typeId','datas'));
        }
        public function admin_move_node(){
            $this->autoRender = false;
            $keys = $kind_node_type = null;             
            if(!empty($this->request->query)){
                $data = $this->request->query;
                if(!empty($data['keys'])){
                    $keys = $data['keys'];  
                }                                
                if(!empty($data['kind_node_type'])){
                    $kind_node_type = $data['kind_node_type'];  
                }
                if(!empty($data['_here'])){
                    $_here = $data['_here'];  
                }                
                $datas['MoveNode']['node_id'] = intval($keys[0]); 
                $datas['MoveNode']['kind_node_type_id'] = $kind_node_type;
                if(!$this->checkDupMoveNode($datas['MoveNode']['node_id'],$datas['MoveNode']['kind_node_type_id'],'MoveNode','Node')){
                    $this->MoveNode->create();
                    $this->MoveNode->save($datas['MoveNode']);
                }                                                                                                             
                $this->setFlash(__d('system', 'Share Node success !!!'), 'success');
                $this->redirect($_here);               
            }            
        }
        public function checkDupMoveNode($node_id=null,$kind_node_type=null,$mode=null,$plugin=null){
            $status = false; 
            $this->loadModel($plugin.'.'.$mode);
            $checkNode = $this->$mode->find('first',array(
                'conditions'=>array($mode.'.node_id'=>$node_id,$mode.'.kind_node_type_id'=>$kind_node_type)
            )); 
            if(!empty($checkNode)){
                $status = true;
            }
            return $status;
        }
        public function admin_edit_management($id=null){        
            $this->Dashboard->exists($this->ManagementLanguage, $id);
            $this->Dashboard->toolbar(__d('system', 'Edit ManagementLanguage #%s', $id), array(
                'save' => array('action' => 'save', 'text' => __d('system', 'Save')),            
                'cancel' => __d('system', 'Cancel')
            ));
            $default_langcode = null;
            $datas = $this->ManagementLanguage->find('first', array('conditions' => array('ManagementLanguage.id' => $id)));
            if(!empty($datas)){
                $default_langcode = $datas['ManagementLanguage']['language_code'].';'.$datas['ManagementLanguage']['prefix'];  
            }
            $this->set(compact('default_langcode','datas'));
            if (!empty($this->request->data)) {
                $data= $this->request->data;
                $language_code = $data['ManagementLanguage']['language_code'];
                if(!empty($language_code)){
                    $language_code = explode(';',$language_code);
                    $data['ManagementLanguage']['language_code'] = $language_code[0]; 
                    $data['ManagementLanguage']['prefix'] = $language_code[1];               
                }            
                $task = strtolower($this->data['Paginate']['task']);
                if (!$task || $task === 'cancel') {
                    $this->Dashboard->redirect(array('action' => 'managemnent_language', 'ext' => false), true);
                } elseif ($this->ManagementLanguage->save($data)) {                               
                    $path = APP."Locale";
                    $folder_old = $datas['ManagementLanguage']['prefix']; 
                    $folder_new = $data['ManagementLanguage']['prefix']; 
                    $results = scandir($path);
                    $is_dir_exits = false;
                    foreach ($results as $result) {
                        if ($result === '.' or $result === '..') continue;
                        if (is_dir($path . '/' . $folder_old)) {                       
                            $is_dir_exits = true;break;
                        }
                    }
                    if($is_dir_exits){
                        $source = APP."Locale\\".$folder_old;
                        $dest = APP."\\Locale\\".$folder_new;  
                        rename($source,$dest);                 
                    }else{$this->admin_config_language($data);}                                               
                    if ($task !== 'remove') {
                        $this->setFlash(__d('system', 'The %s has been updated', __d('system', 'ManagementLanguage')), 'success');
                    }
                    switch ($task) {
                        case 'remove': {
                            $this->Dashboard->redirect(array('action' => 'delete', '?' => array('keys' => $id)), false, true);
                        }
                        case 'apply': {
                            $this->redirect($this->request->here(false));
                        }
                    }
                    $this->setFlash(__d('system', 'The %s has been saved', __d('system', 'Language')), 'success');
                    $this->Dashboard->redirect(array('action' => 'index', 'ext' => false), true);
                } else {
                    $this->setFlash(__d('system', 'The %s could not be updated. Please, try again.', __d('system', 'ManagementLanguage')));
                }
            } else {
                $appController = new AppController();             
                $locale = $appController->defineLocale(); 
                foreach($locale as $key=>$values){
                    $valuesList = $key.';'.$values['locale'];                                 
                    $lstLocale[$valuesList] = $values['language'];                  
                }                                               
                $this->request->data = $this->ManagementLanguage->find('first', array('conditions' => array('ManagementLanguage.id' => $id)));            
                $this->set(compact('lstLocale'));
            }
        }
        public function admin_add_language($id = null){        
            $this->Dashboard->toolbar(__d('system', 'Add Language #%s', $id), array(
                'save' => array('action' => 'save', 'text' => __d('system', 'Save')),            
                'cancel' => __d('system', 'Cancel')
            ));              
            if (!empty($this->request->data)) {
                $data= $this->request->data;
                $language_code = $data['ManagementLanguage']['language_code'];
                if(!empty($language_code)){
                    $language_code = explode(';',$language_code);
                    $data['ManagementLanguage']['language_code'] = $language_code[0]; 
                    $data['ManagementLanguage']['prefix'] = $language_code[1];               
                }   
                $task = strtolower($this->data['Paginate']['task']);
                if (!$task || $task === 'cancel') {
                    $this->Dashboard->redirect(array('action' => 'managemnent_language', 'ext' => false), true);
                } elseif ($this->ManagementLanguage->save($data)) {                               
                    $this->admin_config_language($data);                
                    if ($task !== 'remove') {
                        $this->setFlash(__d('system', 'The %s has been updated', __d('system', 'ManagementLanguage')), 'success');
                    }
                    switch ($task) {
                        case 'remove': {
                            $this->Dashboard->redirect(array('action' => 'delete', '?' => array('keys' => $id)), false, true);
                        }
                        case 'apply': {
                            $this->redirect($this->request->here(false));
                        }
                    }
                    $this->setFlash(__d('system', 'The %s has been saved', __d('system', 'Language')), 'success');
                    $this->Dashboard->redirect(array('action' => 'index', 'ext' => false), true);
                } else {
                    $this->setFlash(__d('system', 'The %s could not be updated. Please, try again.', __d('system', 'ManagementLanguage')));
                }
            }
            $appController = new AppController();             
            $locale = $appController->defineLocale(); 
            foreach($locale as $key=>$values){
                $valuesList = $key.';'.$values['locale'];                                 
                $lstLocale[$valuesList] = $values['language'];                  
            }
            $this->set(compact('lstLocale'));   
        }
        public function admin_set_default_language($id){
            $this->autoRender = false;
            $datas['ManagementLanguage']['active'] = '';
            $localeActive = $this->ManagementLanguage->find('first',array('conditions'=>array('ManagementLanguage.active'=>1)));
            $this->ManagementLanguage->id = $localeActive['ManagementLanguage']['id']; 
            if($this->ManagementLanguage->save($datas)){
                $data['ManagementLanguage']['active'] = 1;
                $this->ManagementLanguage->id = $id;
                $this->ManagementLanguage->save($data);   
            }        
            $this->redirect(array('plugin'=>'node','controller'=>'node','action'=>'management_language'));
        }
        public function admin_management_subsite($id=null){
            $this->Dashboard->toolbar(__d('system', 'Management Subsite'), array(
                'add' => array('action' => 'add', 'text' => __d('system', 'Add')),          
                //'cancel' => __d('system', 'Cancel')
            ));        
            if (!empty($this->data['Paginate'])) {                        
                $data = array_merge(array('task' =>$id, 'id' => array(0)), $this->data['Paginate']);                      
                switch (strtolower($data['task'])) {
                    case 'add': {                        
                        $this->Dashboard->redirect(array('plugin'=>'node','controller'=>'node','action' => 'add_subsite'));
                    }                                
                }
                $dataPaginate = $this->request->data;
                $prefix = array();
                $complete = null;
                $dataPaginate = $this->request->data;
                if(!empty($dataPaginate['Paginate']['chk_node_type'])){
                    $chk_node_type = $dataPaginate['Paginate']['chk_node_type'];
                    foreach($chk_node_type as $k=>$v){
                        $this->Subsite->id = $v;
                        if($this->Subsite->exists()){
                            $this->Subsite->delete();
                        }                     
                    } 
                    $this->Dashboard->redirect(array('action' => 'admin_management_subsite', 'ext' => false), true);
                } 
            }
            else
            {
                $subsite = $this->Subsite->find('all');            
                $this->loadModel('Block.BlockRegion');
                $listBlockbyRegion = $this->BlockRegion->find('all',array(
                    'recursive'=>-1
                ));
                if(!empty($listBlockbyRegion)){
                    foreach($listBlockbyRegion as $k=>$v){
                        $theme = $v['BlockRegion']['theme'];
                        $theme = explode('.',$theme);
                        if(!empty($theme[2])){
                            foreach($subsite as $x=>$y)                        
                            {                                                       
                                if(($theme[2]==$y['Subsite']['id'])||(!empty($subSiteRole))){$subsite[$x]['Subsite']['exst'] = 1; break;}
                            }
                        }                                         
                    }   
                }
                $subSiteRole = $this->Role->find('all',array(
                    'recursive'=>-1
                ));
                if(!empty($subSiteRole)){
                    foreach($subSiteRole as $k=>$v){                     
                        foreach($subsite as $x=>$y)                        
                        {                                                                               
                            if($v['Role']['subsite'] == $y['Subsite']['id']){$subsite[$x]['Subsite']['exst'] = 1;break;}
                        }     
                    }                 
                }                     
                $this->set(compact('subsite'));          
            }                
        }
        public function admin_add_subsite($id=null){
            $this->Dashboard->toolbar(__d('system', 'Add Subsite #%s', $id), array(
                'save' => array('action' => 'save', 'text' => __d('system', 'Save')),            
                'cancel' => __d('system', 'Cancel')
            ));              
            if (!empty($this->request->data)) {
                $data= $this->request->data;                       
                $task = strtolower($this->data['Paginate']['task']);
                if (!$task || $task === 'cancel') {
                    $this->Dashboard->redirect(array('action' => 'management_subsite', 'ext' => false), true);
                } elseif ($this->Subsite->save($data)) {                                                                                   $this->setFlash(__d('system', 'The %s has been saved', __d('system', 'Subsite')), 'success');        
                    $this->Dashboard->redirect(array('action' => 'index', 'ext' => false), true);
                } else {
                    $this->setFlash(__d('system', 'The %s could not be updated. Please, try again.', __d('system', 'Management Subsite')));
                }
            }
            else
            {
                $listTheme = Configure::read('App.themes.listTheme');
                $this->set(compact('listTheme'));
            }        
        }
        public function admin_edit_subsite($id=null){
            $this->Dashboard->exists($this->Subsite, $id);
            $this->Dashboard->toolbar(__d('system', 'Edit Management Subsite #%s', $id), array(
                'save' => array('action' => 'save', 'text' => __d('system', 'Save')),            
                'cancel' => __d('system', 'Cancel')
            ));              
            if (!empty($this->request->data)) {
                $data= $this->request->data;                       
                $task = strtolower($this->data['Paginate']['task']);
                if (!$task || $task === 'cancel') {
                    $this->Dashboard->redirect(array('action' => 'management_subsite', 'ext' => false), true);
                } elseif ($this->Subsite->save($data)) {                                                                                   $this->setFlash(__d('system', 'The %s has been updated', __d('system', 'Subsite')), 'success');           
                    $this->Dashboard->redirect(array('action' => 'index', 'ext' => false), true);
                } else {
                    $this->setFlash(__d('system', 'The %s could not be updated. Please, try again.', __d('system', 'Management Subsite')));
                }
            }
            else
            {
                $listTheme = Configure::read('App.themes.listTheme');
                $exits = false;
                $this->request->data = $this->Subsite->find('first',array('conditions'=>array('Subsite.id'=>$id)));
                $this->loadModel('Block.BlockRegion');
                $listBlockbyRegion = $this->BlockRegion->find('all',array(
                    'recursive'=>-1
                ));
                if(!empty($listBlockbyRegion)){
                    foreach($listBlockbyRegion as $k=>$v){
                        $theme = $v['BlockRegion']['theme'];
                        $theme = explode('.',$theme);
                        if(!empty($theme[2])){
                            if($theme[2]==$id){$exits = true;break;}
                        }
                    }   
                }                                    
                $this->set(compact('listTheme','exits'));
            }  
        }
        public function admin_delete_multiple($Model=null,$Action=null) {
            $this->layout = null;
            $Model = $this->request->query['model'];
            $Action = $this->request->query['linkAction'];
            $this->loadModel('Node.'.$Model);       
            if (empty($this->request->query['keys'])) {
                $this->setFlash(__d('system', 'Select elements should be deleted from the list below!', __d('system', $Model)));
            } else {
                $complete = 0;
                $message = null;
                $keys = (array) $this->request->query['keys'];                                    
                foreach ($keys as $key) {
                    if (!$this->$Model->delete($key)) {
                        $message = $this->$Model->getMessage();
                        if (!$message) {
                            $message = __d('system', 'Cannot delete row #%s.', $key);
                        }
                        break;
                    } else {
                        $complete++;
                    }
                }
                if ($message) {
                    $message = __d('system', '<br/>The processing has stopped because : <b>%s</b>', $message);
                }
                $this->setFlash(__d('system', 'The %1$s has been deleted %2$s / %3$s rows.', __d('system',$Model), $complete, count($keys)) . $message, 'warning');
            }
            $this->redirect(array('plugin'=>'node','controller'=>'node','action'=>$Action));
        }
        /**
        * Delete data
        *
        * @return void
        */
        public function admin_delete() {
            $this->layout = null;
            if (empty($this->request->query['keys'])) {
                $this->setFlash(__d('system', 'Select elements should be deleted from the list below!', __d('system', 'Node')));
            } else {
                $complete = 0;
                $message = null;
                $keys = (array) $this->request->query['keys'];
                foreach ($keys as $key) {
                    if (!$this->Node->delete($key)) {
                        $message = $this->Node->getMessage();
                        if (!$message) {
                            $message = __d('system', 'Cannot delete row #%s.', $key);
                        }
                        break;
                    } else {
                        $complete++;
                    }
                }
                if ($message) {
                    $message = __d('system', '<br/>The processing has stopped because : <b>%s</b>', $message);
                }
                $this->setFlash(__d('system', 'The %1$s has been deleted %2$s / %3$s rows.', __d('system', 'Node'), $complete, count($keys)) . $message, 'warning');
            }
            $this->redirect(array('plugin'=>'node','controller'=>'node','action'=>'index'));
        }
        public function admin_delete_list_index() {
            $this->layout = null;
            if (empty($this->request->query['keys'])) {
                $this->setFlash(__d('system', 'Select elements should be deleted from the list below!', __d('system', 'Node')));
            } else {
                $complete = 0;
                $message = null;
                $_here = 'list_index';
                $keys = (array) $this->request->query['keys'];
                $_here = $this->request->query['_here'];                               
                foreach ($keys as $key) {
                    if (!$this->Node->delete($key)) {
                        $message = $this->Node->getMessage();
                        if (!$message) {
                            $message = __d('system', 'Cannot delete row #%s.', $key);
                        }
                        break;
                    } else {
                        $complete++;
                    }
                }
                if ($message) {
                    $message = __d('system', '<br/>The processing has stopped because : <b>%s</b>', $message);
                }
                $this->setFlash(__d('system', 'The %1$s has been deleted %2$s / %3$s rows.', __d('system', 'Node'), $complete, count($keys)) . $message, 'warning');
            }
            $this->redirect($_here);
        }
        public function admin_delete_subsite() {
            $this->layout = null;
            if (empty($this->request->query['keys'])) {
                $this->setFlash(__d('system', 'Select elements should be deleted from the list below!', __d('system', 'Subsite')));
            } else {
                $complete = 0;
                $message = null;
                $keys = (array) $this->request->query['keys'];
                foreach ($keys as $key) {
                    if (!$this->Subsite->delete($key)) {
                        $message = $this->Subsite->getMessage();
                        if (!$message) {
                            $message = __d('system', 'Cannot delete row #%s.', $key);
                        }
                        break;
                    } else {
                        $complete++;
                    }
                }
                if ($message) {
                    $message = __d('system', '<br/>The processing has stopped because : <b>%s</b>', $message);
                }
                $this->setFlash(__d('system', 'The %1$s has been deleted %2$s / %3$s rows.', __d('system', 'Node'), $complete, count($keys)) . $message, 'warning');
            }
            $this->redirect(array('plugin'=>'node','controller'=>'node','action'=>'management_subsite'));
        }
        public function admin_delete_language() {
            $this->layout = null;
            if (empty($this->request->query['keys'])) {
                $this->setFlash(__d('system', 'Select elements should be deleted from the list below!', __d('system', 'ManagementLanguage')));
            } else {
                $complete = 0;
                $message = null;
                $keys = (array) $this->request->query['keys'];
                $this->loadModel('Node.ManagementLanguage');
                foreach ($keys as $key) {
                    $language = $this->ManagementLanguage->find('first',array('conditions'=>array('ManagementLanguage.id'=>$key)));
                    $prefix = array();
                    if(!empty($language)){
                        $prefix = $language['ManagementLanguage']['prefix'];
                    }                                                          
                    if (!$this->ManagementLanguage->delete($key)) {                     
                        $message = $this->ManagementLanguage->getMessage();
                        if (!$message) {
                            $message = __d('system', 'Cannot delete row #%s.', $key);
                        }
                        break;
                    } else {
                        $dir = APP."Locale\\".$prefix;             
                        $this->deleteDir($dir);  
                        $complete++;
                    } 
                }
                if ($message) {
                    $message = __d('system', '<br/>The processing has stopped because : <b>%s</b>', $message);
                }
                $this->setFlash(__d('system', 'The %1$s has been deleted %2$s / %3$s rows.', __d('system', 'ManagementLanguage'), $complete, count($keys)) . $message, 'warning');
            }
            $this->Dashboard->redirect(array('action' => 'management_language', 'ext' => false), true);
        }
        public function admin_delete_action() {
            $this->layout = null;
            if (empty($this->request->query['keys'])) {
                $this->setFlash(__d('system', 'Select elements should be deleted from the list below!', __d('system', 'Management Action')));
            } else {
                $complete = 0;
                $message = null;
                $keys = (array) $this->request->query['keys'];
                $this->loadModel('Node.Action');
                foreach ($keys as $key) {
                    $language = $this->Action->find('first',array('conditions'=>array('Action.id'=>$key)));
                    $prefix = array();                                                                         
                    if (!$this->Action->delete($key)) {                     
                        $message = $this->Action->getMessage();
                        if (!$message) {
                            $message = __d('system', 'Cannot delete row #%s.', $key);
                        }
                        break;
                    } else {                     
                        $complete++;
                    } 
                }
                if ($message) {
                    $message = __d('system', '<br/>The processing has stopped because : <b>%s</b>', $message);
                }
                $this->setFlash(__d('system', 'The %1$s has been deleted %2$s / %3$s rows.', __d('system', 'Management Action'), $complete, count($keys)) . $message, 'warning');
            }
            $this->Dashboard->redirect(array('action' => 'config_action', 'ext' => false), true);
        }
        public static function deleteDir($dirPath) {
            if (! is_dir($dirPath)) {
                throw new InvalidArgumentException("$dirPath must be a directory");
            }
            if (substr($dirPath, strlen($dirPath) - 1, 1) != '/') {
                $dirPath .= '/';
            }
            $files = glob($dirPath . '*', GLOB_MARK);
            foreach ($files as $file) {
                if (is_dir($file)) {
                    self::deleteDir($file);
                } else {
                    unlink($file);
                }
            }
            rmdir($dirPath);
        }
        /**
        * Translate data
        *
        * @return void
        */
        public function admin_translate($id = null, $language = null) {
            $this->Dashboard->exists($this->Node, $id);
            $this->Dashboard->toolbar(__d('system', 'Translate Node #%s', $id), array(
                'save' => array('action' => 'save', 'text' => __d('system', 'Save')),
                'remove' => array('action' => 'remove', 'text' => __d('system', 'Remove')),
                'cancel' => __d('system', 'Cancel')
            ));
            $locale = $this->Dashboard->getLocale($language);
            if (empty($locale)) {
                $this->setFlash(__d('system', 'The language code <b>%s</b> does not exist', $language));
            }
            $translateFields = $this->Dashboard->translateFields($this->Node);
            if (empty($translateFields)) {
                $this->setFlash(__d('system', 'The %s not be configured a multilingual', __d('system', 'Node')));
            }
            if (empty($locale) || empty($translateFields)) {
                $this->Dashboard->redirect(array('action' => 'index', 'ext' => false), true);
            }
            $this->Node->locale = $locale;
            if ($this->request->is('post')) {
                $task = strtolower($this->data['Paginate']['task']);
                if (!$task || $task === 'cancel') {
                    $this->Dashboard->redirect(array('action' => 'index', 'ext' => false), true);
                } elseif ($this->Node->translate($this->request->data)) {
                    if ($task !== 'remove') {
                        $this->setFlash(__d('system', 'The %s has been translated', __d('system', 'Node')), 'success');
                    } else {
                        $this->Dashboard->removeTranslated($this->Node);
                        $this->setFlash(__d('system', 'The translated data of %s has been removed', __d('system', 'Node')), 'success');
                    }
                    switch ($task) {
                        case 'remove':
                        case 'apply': {
                            $this->redirect($this->request->here(false));
                        }
                    }
                    $this->Dashboard->redirect(array('action' => 'index', 'ext' => false), true);
                } else {
                    $this->setFlash(__d('system', 'The %s could not be translated. Please, try again.', __d('system', 'Node')));
                }
            }
            $node = $this->Dashboard->getTranslated($this->Node, $translateFields);
            $this->set(compact('locale', 'language', 'node', 'id'));
        }
        //delete image 
        public function delete_image($id){
            $this->layout= 'ajax';
            $this->autoRender = false;
            $this->Gallery->id = $id;
            $this->Gallery->delete();
        }
        public function admin_install_action(){
            $this->autoRender = false;
            $viewController  = new ViewController();
            $method = $viewController->methods;
            $duplicate = false; 
            $this->loadModel('Node.Action');
            if(!empty($method)){
                $lstAction = $this->Action->find('list',array('fields'=>array('Action.id','Action.action')));           
                if(!empty($lstAction)){
                    foreach($method as $key=>$value){
                        foreach($lstAction as $x=>$y){
                            if(strcmp($y,$value)==0){$duplicate=true;break;}
                        }
                        if(!$duplicate){
                            $this->Action->create();
                            $data['Action']['action'] = $value; 
                            $data['Action']['descriptions'] = 'Nhập mô tả cho action';
                            $this->Action->save($data['Action']);
                        }
                        $duplicate = false;
                    }                              
                }
                else
                {
                    foreach($method as $k=>$v){
                        $this->Action->create();
                        $data['Action']['action'] = $v; 
                        $data['Action']['descriptions'] = 'Nhập mô tả cho action';
                        $this->Action->save($data['Action']); 
                    } 
                }                      
            }        
            $this->redirect(array('plugin'=>'node','controller'=>'node','action'=>'admin_config_action'));
        }
        public function admin_config_action(){
            $this->Dashboard->toolbar(__d('system', 'Management Action'), array(
                'add' => array('action' => 'add', 'text' => __d('system', 'Install Action')),                      
            )); 
            $this->loadModel('Node.Action');
            $action = $this->Action->find('all',array('locale'=>true,'order'=>'Action.id desc'));
            if (!empty($this->data['Paginate'])) {
                $data = array_merge(array('task' => '', 'id' => array(0)), $this->data['Paginate']);
                switch (strtolower($data['task'])) {
                    case 'add': {
                        $this->Dashboard->redirect(array('plugin'=>'node','controller'=>'node','action' => 'admin_install_action'));
                    }                
                }
                $dataPaginate = $this->request->data;
                if(!empty($dataPaginate['Paginate']['chk_node_type'])){
                    $chk_node_type = $dataPaginate['Paginate']['chk_node_type'];
                    foreach($chk_node_type as $k=>$v){
                        $this->Action->id = $v;
                        if($this->Action->exists()){
                            $this->Action->delete();
                        }                     
                    } 
                    $this->Dashboard->redirect(array('action' => 'admin_config_action', 'ext' => false), true);
                }                        
            }
            $this->set(compact('action'));           
        }
        public function admin_edit_config_action($id=null){
            $this->Dashboard->exists($this->Action, $id);
            $this->Dashboard->toolbar(__d('system', 'Edit ManagementLanguage #%s', $id), array(
                'save' => array('action' => 'save', 'text' => __d('system', 'Save')),            
                'cancel' => __d('system', 'Cancel')
            ));
            $this->loadModel('Node.Action') ;
            $datas = $this->Action->find('first',array('condtions'=>array('Action.id'=>$id)));
            $this->set(compact('datas'));       
            if (!empty($this->request->data)) {            
                $data = $this->request->data;                     
                $task = strtolower($this->data['Paginate']['task']);            
                if (!$task || $task === 'cancel') {
                    $this->Dashboard->redirect(array('action' => 'admin_config_action', 'ext' => false), true);
                } elseif ($this->Action->save($data)) {                                                             

                    $this->Dashboard->redirect(array('action' => 'admin_config_action', 'ext' => false), true);
                } else {
                    $this->setFlash(__d('system', 'The %s could not be updated. Please, try again.', __d('system', 'Management Action')));
                }
            } else {        
                $this->request->data = $this->Action->find('first',array('conditions'=>array('Action.id'=>$id)));            
            } 
        }
        public function admin_mn_comment($node_type_id = null,$node_id=null){
            $this->Dashboard->toolbar(__d('system', 'Management Comments'), array(
                'add' => array('action' => 'add', 'text' => __d('system', 'Add New')),          
                'cancel' => __d('system', 'Cancel')
            ));        
            if (!empty($this->data['Paginate'])) {                        
                $data = array_merge(array('task' =>$id, 'id' => array(0)), $this->data['Paginate']);                        
                $dataPaginate = $this->request->data;
                $prefix = array();
                $complete = null;
                if(!empty($dataPaginate['Paginate']['chk_node_type'])){                                
                    $this->Dashboard->redirect(array('action' => 'admin_management_language', 'ext' => false), true);
                } 
            }
            $this->loadModel('View.Comment');
            $comments = $this->Comment->find('all',array(
                'conditions'=>array('Comment.node_id'=>$node_id)
            ));        
            $this->set(compact('comments'));   
        }
        public function set_status($id=null,$status=null){
            $this->layout = 'ajax';       
            $this->loadModel('View.Comment');
            $data['Comment']['id'] = $id; 
            $data['Comment']['status'] = $status;        
            $this->Comment->save($data['Comment']);        
            $comments = $this->Comment->find('first',array(
                'conditions'=>array('Comment.id'=>$id)
            ));  
            $this->set(compact('comments','id'));              
        }
        public function delete_comment($id=null){
            $this->layout = 'ajax';
            $this->autoRender = false;
            $this->loadModel('View.Comment');
            $this->Comment->id = $id;
            $this->Comment->delete();
        }    
        public function admin_management_language($id = null){        
            $this->Dashboard->toolbar(__d('system', 'Management Language'), array(
                'add' => array('action' => 'add', 'text' => __d('system', 'Add New')),          
                //'cancel' => __d('system', 'Cancel')
            ));        
            if (!empty($this->data['Paginate'])) {                        
                $data = array_merge(array('task' =>$id, 'id' => array(0)), $this->data['Paginate']);            
                switch (strtolower($data['task'])) {
                    case 'add': {                        
                        $this->Dashboard->redirect(array('plugin'=>'node','controller'=>'node','action' => 'add_language'));
                    }                                
                }
                $dataPaginate = $this->request->data;
                $prefix = array();
                $complete = null;
                if(!empty($dataPaginate['Paginate']['chk_node_type'])){
                    $chk_node_type = $dataPaginate['Paginate']['chk_node_type'];
                    $dir = APP."Locale\\".$prefix; 
                    foreach ($chk_node_type as $key) {
                        $language = $this->ManagementLanguage->find('first',array('conditions'=>array('ManagementLanguage.id'=>$key)));                   
                        if(!empty($language)){
                            $prefix = $language['ManagementLanguage']['prefix'];
                        }                                                          
                        if (!$this->ManagementLanguage->delete($key)) {                     
                            $message = $this->ManagementLanguage->getMessage();
                            if (!$message) {
                                $message = __d('system', 'Cannot delete row #%s.', $key);
                            }
                            break;
                        } else {
                            $dir = APP."Locale\\".$prefix;             
                            $this->deleteDir($dir);  
                            $complete++;
                        } 
                    }                  
                    $this->Dashboard->redirect(array('action' => 'admin_management_language', 'ext' => false), true);
                } 
            }
            $language = $this->ManagementLanguage->find('all');
            $this->set(compact('language'));        
        }
        public function admin_config_language($data = array()){            
            if((!empty($data))){
                $prefix = $data['ManagementLanguage']['prefix'];
                $source = APP."Locale\\default\\LC_MESSAGES";
                $destination = APP.'Locale\\'.$prefix."\\LC_MESSAGES";            
                if (!file_exists($destination)){
                    mkdir($destination, 0777, true);            
                }
                if (file_exists($source)){
                    if(is_dir($destination)){
                        $this->copyFolder($source,$destination);
                    }        
                }   
            }            
        }
        //Manage config mail
        public function admin_manage_config_mail($id=null){ 
            $this->loadModel('Node.ConfigMail');            
            $this->Dashboard->toolbar(__d('system', 'Management Config Mail'), array(
                'add' => array('action' => 'add', 'text' => __d('system', 'Add')),            
                //'cancel' => __d('system', 'Cancel')
            ));    
            if (!empty($this->data['Paginate'])) {                        
                $data = array_merge(array('task' =>$id, 'id' => array(0)), $this->data['Paginate']);            
                switch (strtolower($data['task'])) {
                    case 'add': {                        
                        $this->Dashboard->redirect(array('plugin'=>'node','controller'=>'node','action' => 'admin_add_config_mail'));
                    }                                
                }             
            }
            $mails = $this->ConfigMail->find('all',array(
                'order'=>'ConfigMail.sub_site_id asc'
            ));
            $listSubSite = Configure::read('App.themes.listSubSite');       
            if(!empty($mails)){
                foreach($mails as $k=>$v){
                    if(!empty($v['ConfigMail']['sub_site_id'])){
                        $mails[$k]['ConfigMail']['sub_site_name'] = $listSubSite[$v['ConfigMail']['sub_site_id']];   
                    }
                    else{
                        $mails[$k]['ConfigMail']['sub_site_name'] = '';
                    }                            
                }
            }             
            $this->set(compact('mails')); 
        }
        public function admin_add_config_mail($id=null){
            $this->loadModel('Node.ConfigMail');
            $this->Dashboard->toolbar(__d('system', 'Add Config Mail #%s', $id), array(
                'save' => array('action' => 'save', 'text' => __d('system', 'Save')),            
                'cancel' => __d('system', 'Cancel')
            )); 
            if (!empty($this->request->data)) {
                $data= $this->request->data;                       
                $task = strtolower($this->data['Paginate']['task']);
                if (!$task || $task === 'cancel') {
                    $this->Dashboard->redirect(array('action' => 'manage_config_mail', 'ext' => false), true);
                } elseif ($this->ConfigMail->save($data)) { 
                    $this->setFlash(__d('system', 'The %s has been saved', __d('system', 'Config Mail')), 'success');                                              
                    $this->Dashboard->redirect(array('action' => 'index', 'ext' => false), true);
                } else {
                    $this->setFlash(__d('system', 'The %s could not be updated. Please, try again.', __d('system', 'Management Config Mail')));
                }
            }
            $listSubSite = Configure::read('App.themes.listSubSite');             
            $this->set(compact('listSubSite'));
        }
        public function admin_edit_config_mail($id=null){
            $this->loadModel('Node.ConfigMail');  
            $data = array();  
            $default = 0;
            $this->Dashboard->exists($this->ConfigMail, $id);
            $this->Dashboard->toolbar(__d('system', 'Edit Config Mail #%s', $id), array(
                'save' => array('action' => 'save', 'text' => __d('system', 'Save')),            
                'cancel' => __d('system', 'Cancel')
            ));
            if (!empty($this->request->data)) {
                $data= $this->request->data;                       
                $task = strtolower($this->data['Paginate']['task']);
                if (!$task || $task === 'cancel') {
                    $this->Dashboard->redirect(array('action' => 'manage_config_mail', 'ext' => false), true);
                } elseif ($this->ConfigMail->save($data)) {
                    $this->setFlash(__d('system', 'The %s has been saved', __d('system', 'Config Mail')), 'success');                                               
                    $this->Dashboard->redirect(array('action' => 'index', 'ext' => false), true);
                } else {
                    $this->setFlash(__d('system', 'The %s could not be updated. Please, try again.', __d('system', 'Management Config Mail')));
                }
            }else{
                $this->request->data = $this->ConfigMail->find('first',array(
                    'conditions'=>array('ConfigMail.id'=>$id)
                )); 
                $data = $this->request->data;
                $listSubSite = Configure::read('App.themes.listSubSite');
                if(!empty($data)){
                    if($data['ConfigMail']['type_mail']==1){
                        $default = 1;
                    } 
                }            
            }                     
            $this->set(compact('default','listSubSite'));           
        }
        //End
        //set default config mail
        public function admin_set_default_mail($id,$sub_site_id=null){
            $this->loadModel('Node.ConfigMail');
            $this->autoRender = false;
            $datas['ConfigMail']['set_default'] = '';
            $set_default = $this->ConfigMail->find('first',array('conditions'=>array('ConfigMail.set_default'=>1,'ConfigMail.sub_site_id'=>$sub_site_id)));
            if(!empty($set_default)){
                $this->ConfigMail->id = $set_default['ConfigMail']['id']; 
                if($this->ConfigMail->save($datas)){
                    $data['ConfigMail']['set_default'] = 1;
                    $this->ConfigMail->id = $id;
                    if($this->ConfigMail->save($data))
                    {
                        $this->setFlash(__d('system', 'The %s has been saved', __d('system', 'ConfigMail')), 'success');
                    }else{
                        $this->setFlash(__d('system', 'The %s could not be saved. Please, try again.', __d('system', 'ConfigMail')), 'error');
                    }                                
                }           
            }
            else{
                $this->setFlash(__d('system', 'The %s has been saved', __d('system', 'Node')), 'success');
            }        
            $this->redirect(array('plugin'=>'node','controller'=>'node','action'=>'manage_config_mail'));
        }
        //Manage Contact
        public function admin_manage_contact($id=null){
            $this->loadModel('Node.MoveContact'); 
            $conds = array(); 
            if (!empty($this->data['Paginate'])) {
                $dataPaginate = $this->request->data;
                $prefix = array();
                $complete = 0;
                $message = null;
                if(!empty($dataPaginate['Paginate']['chk_node_type'])){
                    $chk_node_type = $dataPaginate['Paginate']['chk_node_type'];
                    foreach ($chk_node_type as $key) {
                        $language = $this->Contact->find('first',array('conditions'=>array('Contact.id'=>$key)));
                        if (!$this->Contact->delete($key)) {
                            if (!$message) {
                                $message = __d('system', 'Cannot delete row #%s.', $key);
                            }
                            break;
                        } else {
                            $complete++;
                        } 
                    }
                    if ($message) {
                        $message = __d('system', '<br/>The processing has stopped because : <b>%s</b>', $message);
                    }
                    $this->setFlash(__d('system', 'The %1$s has been deleted %2$s / %3$s rows.', __d('system', 'Contact'), $complete, count($chk_node_type)) . $message, 'warning');                                                    
                }
                elseif(!empty($dataPaginate['Paginate']['statusNode'])){
                    $statusNode = intval($dataPaginate['Paginate']['statusNode']); 
                    if($statusNode==1){
                        $conds = array('Contact.status'=>1);
                    }elseif($statusNode==2){
                        $conds = array('Contact.status'=>0);
                    }elseif($statusNode==3){
                        $conds = array('Contact.important'=>1);
                    }elseif($statusNode==4){
                        $conds = array('Contact.favourite'=>1);
                    }
                    $contacts = $this->Contact->find('all',array(
                        'conditions'=>$conds
                    ));
                    $this->set(compact('contacts'));
                }
            }else{
                $contacts = $this->Contact->find('all',array(
                    'order'=>'Contact.created desc'
                ));
                $this->set(compact('contacts'));
            }
        }     
        //End
        //Manage Contact
        public function admin_edit_contact($id=null){
            $this->loadModel('Node.MoveContact'); 
            $this->loadModel('View.Contact');
            $subSiteModify = $subsite_current = array();
            $data = array();  
            $default = 0;
            $this->Dashboard->exists($this->Contact, $id);
            $this->Dashboard->toolbar(__d('system', 'Edit Contact #%s', $id), array(
                'save' => array('action' => 'save', 'text' => __d('system', 'Save')),
                'cancel' => __d('system', 'Cancel')
            ));             
            $subSiteModify = Configure::read('App.themes.listSubSite'); 
            $subsite_current = Configure::read('App.themes.subsite_current');
            /*if((!empty($subSiteModify))&&(!empty($subsite_current))){
            foreach($subSiteModify as $k=>$v){                                         
            if(strcmp($k,$subsite_current['id'])==0){
            unset($subSiteModify[$k]);break;                          
            }                      
            }                    
            }*/ 
            if (!empty($this->request->data)) {
                $data= $this->request->data;                       
                $task = strtolower($this->data['Paginate']['task']);
                if (!$task || $task === 'cancel') {
                    $this->Dashboard->redirect(array('action' => 'admin_manage_contact', 'ext' => false), true);
                }else{
                    //for event save contact move 
                    $this->MoveContact->deleteAll(array('MoveContact.contact_id'=>$id));            
                    if(!empty($data['Contact']['is_move'])){
                        $lstMove = $data['Contact']['is_move'];                                                                                          
                        $datas['MoveContact']['contact_id'] = $id;                       
                        foreach($lstMove as $k=>$v){
                            $this->MoveContact->create(); 
                            $datas['MoveContact']['subsite_id'] = $v;                        
                            $this->MoveContact->save($datas['MoveContact']);                               
                        }
                        unset($data['Contact']['is_move']);                                                
                    }
                    //End
                    if ($this->Contact->save($data)) {
                        $this->setFlash(__d('system', 'The %s has been saved', __d('system', 'Contact')), 'success');                                               
                        $this->Dashboard->redirect(array('action' => 'index', 'ext' => false), true);
                    } else {
                        $this->setFlash(__d('system', 'The %s could not be updated. Please, try again.', __d('system', 'Management Contact')));
                    }   
                }                
            }else{
                $this->request->data = $this->Contact->find('first',array(
                    'conditions'=>array('Contact.id'=>$id)
                )); 
                $data = $this->request->data;
                if((!empty($data))&&($data['Contact']['status']==0)){
                    $datas['Contact']['status'] = 1;
                    $this->Contact->id = $id;
                    $this->Contact->save($datas['Contact']);    
                }                                                                                         
            }                                
            $this->set(compact('default','subSiteModify','id'));                    
        }    
        public function admin_view_contact($id=null){            
            $this->loadModel('Node.MoveContact'); 
            $this->loadModel('View.Contact');
            $subSiteModify = $subsite_current = array();                            
            $data = array();  
            $default = 0;
            $this->Dashboard->exists($this->Contact, $id);
            $this->Dashboard->toolbar(__d('system', 'Edit Contact #%s', $id), array(                              
                'cancel' => __d('system', 'Back')
            )); 
            if (!empty($this->request->data)){
                $this->Dashboard->redirect(array('action' => 'admin_manage_contact', 'ext' => false), true);
            }                                 
            $this->request->data = $this->Contact->find('first',array(
                'conditions'=>array('Contact.id'=>$id)
            ));                                                                                                               
        } 
        //End
        //Manage Question
        public function admin_manage_question($id=null){
            $subSiteModify = array();
            $sub_site = array();
            $this->loadModel('Node.MoveQuestion');                       
            $subsite_current = Configure::read('App.themes.subsite_current');
            $this->loadModel('Node.Question');            
            $this->Dashboard->toolbar(__d('system', 'Management Question'), array(
                'add' => array('action' => 'add', 'text' => __d('system', 'Add')),            
                'cancel' => __d('system', 'Cancel')
            ));             
            if (!empty($this->data['Paginate'])) {                                                                                              
                $dataPaginate = $this->request->data;
                $prefix = array();
                $complete = 0;
                $message = null;                            
                switch (strtolower($dataPaginate['Paginate']['task'])) {
                    case 'add': {                        
                        $this->Dashboard->redirect(array('plugin'=>'node','controller'=>'node','action' => 'admin_add_question'));
                    }                                
                } 
                if(!empty($dataPaginate['Paginate']['chk_node_type'])){
                    $chk_node_type = $dataPaginate['Paginate']['chk_node_type'];                                    
                    foreach ($chk_node_type as $key) {                        
                        if (!$this->Question->delete($key)) {                                             
                            if (!$message) {
                                $message = __d('system', 'Cannot delete row #%s.', $key);
                            }
                            break;
                        } else {                         
                            $complete++;
                        } 
                    }
                    if ($message) {
                        $message = __d('system', '<br/>The processing has stopped because : <b>%s</b>', $message);
                    }
                    $this->setFlash(__d('system', 'The %1$s has been deleted %2$s / %3$s rows.', __d('system', 'Question'), $complete, count($chk_node_type)) . $message, 'warning');                                                                 
                    $this->redirect(array('action' => 'admin_manage_question', 'ext' => false), true);
                }  

            }          
            $subsite_current = Configure::read('App.themes.subsite_current');
            if(empty($subsite_current)){
                $subsite_current['id'] = -1; 
            } 
            $movelistQuestions = $this->MoveQuestion->find('all',array(
                'conditions'=>array('MoveQuestion.subsite_id'=>$subsite_current['id'])                  
            ));                                             
            $listQuestions = $this->Question->find('all',array(
                'conditions'=>array('Question.sub_site'=>$subsite_current['id']),
                'order'=>'Question.created desc'
            ));                                             
            $this->set(compact('listQuestions','movelistQuestions'));   
        } 
        public function admin_add_question($id=null){
            $data = array();                         
            $default = 0;
            $listSubSite = Configure::read('App.themes.listSubSite');            
            $this->Dashboard->toolbar(__d('system', 'Add Question #%s', $id), array(
                'save' => array('action' => 'save', 'text' => __d('system', 'Save')),            
                'cancel' => __d('system', 'Cancel')
            ));           
            if (!empty($this->request->data)) {                 
                $data = $this->request->data;
                $task = strtolower($this->data['Paginate']['task']);                
                if (!$task || $task === 'cancel') {
                    $this->Dashboard->redirect(array('action' => 'index', 'ext' => false), true);
                }else{                    
                    $this->Question->create();                    
                    if ($this->Question->save($data)) 
                    {                        
                        $this->setFlash(__d('system', 'The %s has been saved', __d('system', 'Question')), 'success');                                               
                        $this->Dashboard->redirect(array('action' => 'index', 'ext' => false), true);
                    } 
                    else 
                    {
                        $this->setFlash(__d('system', 'The %s could not be updated. Please, try again.', __d('system', 'Management Question')));
                    }
                }                                                                                                    
            }else{
                $this->request->data = $this->Question->find('first',array(
                    'conditions'=>array('Question.id'=>$id)
                )); 
                $data = $this->request->data;                                                        
            }                     
            $this->set(compact('data','id')); 
        }
        public function admin_not_received_question($id=null){
            $this->autoRender = false;
            $keys = $this->request->query;            
            if(!empty($keys)){
                $this->loadModel('Node.MoveQuestion');
                foreach($keys as $k=>$v){
                    $this->MoveQuestion->id = $v;
                    $this->MoveQuestion->delete();   
                }                
            }
            $this->Dashboard->redirect(array('action' => 'admin_manage_question', 'ext' => false), true);           
        } 
        public function admin_not_received_multiple($id=null){
            $this->autoRender = false;
            $keys = $model = $plugin = $linkAction  = -1;  
            $query = $this->request->query;
            if(isset($query['keys'])){
                $keys = $query['keys'];
            }                                    
            if(isset($query['model'])){
                $model = $query['model'];
            }            
            if(isset($query['plugin'])){
                $plugin = $query['plugin'];
            }             
            if(isset($query['linkAction'])){
                $linkAction = $query['linkAction'];
            }                                    
            $this->loadModel($plugin . '.'. $model);
            foreach($keys as $k=>$v){
                $this->$model->id = $v;
                $this->$model->delete();   
            }                          
            $this->Dashboard->redirect(array('action' => $linkAction, 'ext' => false), true);           
        }
        public function admin_answer_question($id=null){            
            $data = array();
            $this->loadModel('Node.MoveQuestion');
            $subSiteModify = $subsite_current = array(); 
            $subSiteModify = Configure::read('App.themes.listSubSite'); 
            $subsite_current = Configure::read('App.themes.subsite_current');
            /*if((!empty($subSiteModify))&&(!empty($subsite_current))){
            foreach($subSiteModify as $k=>$v){                                         
            if(strcmp($k,$subsite_current['id'])==0){
            unset($subSiteModify[$k]);break;                          
            }                      
            }                    
            }*/   
            $default = 0;
            $listSubSite = Configure::read('App.themes.listSubSite');
            $this->Dashboard->exists($this->Question, $id);
            $this->Dashboard->toolbar(__d('system', 'Replay Question #%s', $id), array(
                'save' => array('action' => 'save', 'text' => __d('system', 'Save')),            
                'cancel' => __d('system', 'Cancel')
            ));
            if (!empty($this->request->data)) {
                $data = $this->request->data;                                                    
                $task = strtolower($this->data['Paginate']['task']);
                //for event save question move 
                $this->MoveQuestion->deleteAll(array('MoveQuestion.question_id'=>$id));            
                if(!empty($data['Question']['is_move'])){
                    $lstMove = $data['Question']['is_move'];                                                                                          
                    $datas['MoveQuestion']['question_id'] = $id;                       
                    foreach($lstMove as $k=>$v){
                        $this->MoveQuestion->create(); 
                        $datas['MoveQuestion']['subsite_id'] = $v;                        
                        $this->MoveQuestion->save($datas['MoveQuestion']);                               
                    }
                    unset($data['Question']['is_move']);                                                
                }
                //End                                      
                if ($this->Question->save($data)) 
                {
                    $this->setFlash(__d('system', 'The %s has been saved', __d('system', 'Question')), 'success');                                               
                    $this->Dashboard->redirect(array('action' => 'index', 'ext' => false), true);
                } 
                else 
                {
                    $this->setFlash(__d('system', 'The %s could not be updated. Please, try again.', __d('system', 'Management Question')));
                }                               
            }else{
                $this->request->data = $this->Question->find('first',array(
                    'conditions'=>array('Question.id'=>$id)
                )); 
                $data = $this->request->data;                                                        
            }                     
            $this->set(compact('data','subSiteModify','id'));   
        }   
        public function admin_view_answer_question($id=null){            
            $data = array();                        
            $this->Dashboard->exists($this->Question, $id);
            $this->Dashboard->toolbar(__d('system', 'Replay Question #%s', $id), array(                          
                'cancel' => __d('system', 'Back')
            )); 
            if (!empty($this->request->data)){
                $task = strtolower($this->data['Paginate']['task']);
                if (!$task || $task === 'cancel') {
                    $this->Dashboard->redirect(array('action' => 'manage_config_mail', 'ext' => false), true);
                } 
            }           
            $this->request->data = $this->Question->find('first',array(
                'conditions'=>array('Question.id'=>$id)
            ));             
        }   
        //End
        public function management_subsite(){

        } 
        public static function copyFolder($src, $dest) {
            $path = realpath($src);
            $objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::SELF_FIRST);
            /** SplFileInfo $object*/
            foreach($objects as $name => $object)
            {
                $startsAt = substr(dirname($name), strlen($src));
                self::mkDir($dest.$startsAt);
                if(is_writable($dest.$startsAt) and $object->isFile())
                {
                    copy((string)$name, $dest.$startsAt.DIRECTORY_SEPARATOR.basename($name));
                }
            }
        }
        private static function mkDir($folder, $perm=0777) {
            if(!is_dir($folder)) {
                mkdir($folder, $perm);
            }
        }
        //ajaxfile upload image
        public function admin_upload(){
            Configure::write('debug', 3);
            if(isset($_SERVER["CONTENT_LENGTH"])){
                print_r($_SERVER["CONTENT_LENGTH"]);
                //die;
            }
            $this->autoRender = false;
            App::uses('qqFileUploader','Vendor');

            // list of valid extensions, ex. array("jpeg", "xml", "bmp")
            $allowedExtensions = array();

            // max file size in bytes
            $sizeLimit = 24 * 1024 * 1024;
            $uploaddir = WWW_ROOT.'img\nurseries\\';

            $uploader = new qqFileUploader($allowedExtensions, $sizeLimit);

            $result = $uploader->handleUpload($uploaddir);
            // to pass data through iframe you will need to encode all html tags


            echo htmlspecialchars(json_encode($result), ENT_NOQUOTES);
            exit;
        }
        public function routerAction($node_type_id=null){
            $result = null;
            switch($node_type_id){
                case Introduction_detail:$result = '/trang-truong-gioi-thieu';break;
                case Education:$result = '/trang-truong-dao-tao';break;
                case Student_affairs:$result = '/trang-truong-cong-tac-sinh-vien';break;
                case Scientific_research:$result = '/trang-truong-nghien-cuu-khoa-hoc';break;
                case International_cooperation:$result = '/hop-tac-quoc-te';break;
                case Library:$result = '/trang-truong-thu-vien';break;
                case Recruitment:$result = '/trang-truong-thu-vien';break;
                case News:$result = '/trang-truong-chi-tiet-tin-tuc';break;            
                default:
            }
            return $result;    
        }
        public function load_node_type($subsite=null,$role_id = null){
            $this->layout = 'ajax'; 
            $this->loadModel('Node.NodeType');
            $rolesNodeType = $this->NodeType->find('all',array(
                'conditions'=>array('NodeType.sub_site'=>$subsite,'NodeType.enabled'=>1)
            ));
            $this->set(compact('rolesNodeType','role_id'));            
        } 
        public function load_field($kind_node_type=null,$node_type_id=null){
            $this->layout = 'ajax';  
            $field_name = $newListField = array();
            $duplicate = false;
            $this->loadModel('Node.Field');
            $this->loadModel('Node.NodeType');
            $listField = array();
            $listNodeType = $this->NodeType->find('list',array(
                'conditions'=>array('NodeType.kind_node_type_id'=>$kind_node_type),
                'fields'=>array('NodeType.id','NodeType.name'),
                'recursive'=>-1
            )); 
            if(!empty($listNodeType)){
                $listField = $this->Field->find('all',array(
                    'conditions'=>array('Field.type_id'=>array_keys($listNodeType)),
                    'recursive'=>-1
                ));
                if(!empty($listField)){
                    foreach($listField as $k=>$value){
                        if(count($newListField)>1){
                            foreach($newListField as $x=>$y){
                                if(strcmp($y['Field']['name'],$value['Field']['name'])==0){
                                    $duplicate = true; break;
                                }
                            }
                        }
                        if(!$duplicate){$newListField[] = $value;}                                              
                    }                    
                }
            } 
            $listField = $newListField;                                    
            $this->set(compact('listField','node_type_id'));
        } 
        public function add_important_contact($id=null,$important=null){
            $this->autoRender = false;
            $this->loadModel('View.Contact');
            $datas['Contact']['important'] = $important;
            $this->Contact->id = $id;
            $this->Contact->save($datas['Contact']);
        }       
        public function add_favourite_contact($id=null,$favourite=null){
            $this->autoRender = false;
            $this->autoRender = false;
            $this->loadModel('View.Contact');
            $datas['Contact']['favourite'] = $favourite;
            $this->Contact->id = $id;
            $this->Contact->save($datas['Contact']);
        }
        public function admin_edit_image($typeId, $id = null, $gal_id = null) {        
            $this->Dashboard->exists($this->Node, $id);
            $this->Dashboard->toolbar(__d('system', 'Edit Node #%s', $id), array(
                'save' => array('action' => 'save', 'text' => __d('system', 'Save')),
                'remove' => array('action' => 'remove', 'text' => __d('system', 'Delete')),
                'cancel' => __d('system', 'Cancel')
            ));
            $datas = $this->Node->find('first', array('conditions' => array('Node.id' => $id)));
            $this->loadModel('Node.Field');
            $fields = $this->Field->find('all', array('recursive' => -1, 'order' => 'id ASC', 'conditions' => array('type_id' => $typeId)));
            if (!empty($fields)) {
                foreach ($fields as &$field) {
                    $field['Field']['params'] = unserialize($field['Field']['params']);
                }
            }
            $this->loadModel('Node.Term');
            $listTerms = $this->Term->generateTreeList(array('Term.type_id' => $typeId), null, null, '&nbsp;&nbsp;&nbsp;|-&nbsp;');
            $node_type = $this->NodeType->read(null,$typeId);
            if(empty($node_type))
            {
                $this->Dashboard->redirect(array('action' => 'index', 'ext' => false), true); 
            }
            if($node_type['NodeType']['set_image']==1)
            {

                $galleries= $this->Gallery->find('all', array(
                    'conditions'=> array('Gallery.node_id'=> $id),
                    'fields'=> array('Gallery.id', 'Gallery.path','Gallery.priority')
                ));
                $this->set('galleries', $galleries); 

                $this->set('images', 'images');
                $gall_details= $this->Gallery->find('first', array(
                    'conditions'=> array('Gallery.id'=> $gal_id),
                    'fields'=> array('Gallery.id', 'Gallery.path','Gallery.priority')
                ));
                $this->set('gall_details', $gall_details);
            }   

            $nodeTerm = $this->Node->NodeTerm->find('first', array('conditions' => array('NodeTerm.node_id' => $id)));
            $this->set(compact('listTerms', 'nodeTerm'));
            if (!empty($this->request->data)) {
                $task = strtolower($this->data['Paginate']['task']);             
                if (!$task || $task === 'cancel') {
                    $this->Dashboard->redirect(array('action' => 'index', 'ext' => false), true);
                } elseif ($this->Node->save($this->request->data)) {               
                    //update infomation image
                    $data = $this->request->data;

                    if (!empty($data)) {
                        $save_data = $data['Gallery'] ;
                        $this->Gallery->id =  $gal_id;
                        $this->Gallery->path =  $save_data['path'] ;
                        $this->Gallery->save($save_data);
                        $this->redirect(array('action' => 'edit',$typeId,$id));
                    }

                    //end update infomation image    
                    if (!empty($this->request->data['Node']['term_id'])) {
                        $term = array();
                        $term['NodeTerm']['id'] = $this->request->data['Node']['node_term_id'];
                        $term['NodeTerm']['node_id'] = $id;
                        $term['NodeTerm']['term_id'] = $this->request->data['Node']['term_id'];
                        $this->Node->NodeTerm->save($term);
                    }else{
                        $this->NodeTerm->id = $this->request->data['Node']['node_term_id'];
                        $this->NodeTerm->delete();
                    }
                    if ($task !== 'remove') {
                        $this->setFlash(__d('system', 'The %s has been updated', __d('system', 'Node')), 'success');
                    }
                    switch ($task) {
                        case 'remove': {
                            $this->Dashboard->redirect(array('action' => 'delete', '?' => array('keys' => $id)), false, true);
                        }
                        case 'apply': {
                            $this->redirect($this->request->here(false));
                        }
                    }
                    $this->Dashboard->redirect(array('action' => 'index', 'ext' => false), true);
                } else {
                    $this->setFlash(__d('system', 'The %s could not be updated. Please, try again.', __d('system', 'Node')));
                }
            } else {
                $this->request->data = $this->Node->find('first', array('conditions' => array('Node.id' => $id)));
            }
            $this->set(compact('id', 'fields','typeId','datas'));
        } 
        //-------------------------------------------------------------------//


    }

?>
